package com.gongJiao.controller;


import com.gongJiao.Vo.AdminVehicleVo;
import com.gongJiao.Vo.PageResult;
import com.gongJiao.Vo.TypeVo;
import com.gongJiao.Vo.VehicleVo;
import com.gongJiao.common.Result;
import com.gongJiao.entity.Route;
import com.gongJiao.entity.Type;
import com.gongJiao.entity.Vehicle;
import com.gongJiao.service.TypeService;
import com.gongJiao.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  公交分类前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/type")
public class TypeController {
    @Autowired
    TypeService typeService;
    @Autowired
    VehicleService vehicleService;
    /**
     * 获取路线信息集合
     * @param session
     * @return
     */
    @GetMapping("getTypeList")
    @ResponseBody
    public Result getTypeList(HttpSession session){
        List<Type> typeList=typeService.getTypeList();
        session.setAttribute("typeList",typeList);
        return Result.success("成功");
    }

    /**
     * 后台分页查询分类信息列表
     * @return
     */
    @GetMapping("getTypePageList")
    @ResponseBody
    public PageResult<Type> getTypePageList(TypeVo typeVo){
        PageResult<Type> pageResult=new PageResult<Type>();
            //查询所有数据
            List<Type> list=typeService.getTypeListByName(typeVo);
            //查询总记录数
            Integer count=typeService.getCountByName(typeVo);
            return pageResult.success(list,count);
    }

    /**
     * 添加分类
     * @param type
     * @return
     */
    @PostMapping("addType")
    @ResponseBody
    public Result addVehicle(Type type){
        boolean save = typeService.save(type);
        if (save) {
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }

    /**
     * 根据id获取分类信息
     * @param id
     * @return
     */
    @GetMapping("/getType/{id}")
    public String getTypeById(@PathVariable Long id, Model model){
        //获取分类信息
        Type type=typeService.getTypeById(id);
        model.addAttribute("type",type);
        return "admin/typeEdit";
    }

    /**
     * 修改信息
     * @param type
     * @return
     */
    @PostMapping("updateType")
    @ResponseBody
    public Result updateType(Type type){
        Boolean flag=typeService.updateType(type);
        if (flag) {
            return Result.success("分类信息修改成功！");
        }
        return Result.fail("分类信息修改失败！");
    }

    /**
     * 删除分类信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteTypeByIds(@PathVariable("ids") Long ids){
        //查询分类是否被使用
        String vehicNames= vehicleService.getVehicleByTypeId(ids);
        if(vehicNames==null){
            typeService.deleteTypeByIds(ids);
            return Result.success("删除分类成功！");
        }else {
            return Result.fail("该分类已经被公交："+vehicNames+"使用无法进行删除！");
        }
    }

    /**
     * 判断分类名是否可用
     * @param name
     * @return
     */
    @GetMapping("/judgeAvailable")
    @ResponseBody
    public Result judgeAvailable(String name){
        if ("".equals(name)){
            return Result.success("");
        }
        Type type=typeService.getTypeByName(name);
        if (type == null) {
            return Result.success("分类名可用");
        }
        return Result.fail("分类名已存在不可用");
    }
}

